﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using log4net;

namespace Hope.Gallery.Core
{
    public static class DirectoryHelper
    {

      

        /// <summary>
        /// Get upload path for an album 
        /// </summary>
        /// <param name="albumName"></param>
        /// <returns></returns>
        public static string GetPhysicalUploadPathForAlbum(string albumName)
        {
            string directoryPath = string.Format("~/{0}/{1}", ConfigKeys.UploadRoot, albumName);
            Logger.Instance.Info("Relative directory Path: " + directoryPath);

            string path = HttpContext.Current.Server.MapPath(directoryPath);
            Logger.Instance.Info("Path on disk: " + path);

            if (!Directory.Exists(path))
            {
                Logger.Instance.Info("Directory not exist");
                try
                {
                    Directory.CreateDirectory(path);
                    Logger.Instance.Info("Directory created:" + path);
                }
                catch (Exception ex)
                {
                    Logger.Instance.Info("Directory wasn't created:" + path);
                    Logger.Instance.Error(ex);
                }
            }

            return path;
        }

        /// <summary>
        /// Get path to the file which is already uploaded
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="album"></param>
        /// <returns></returns>
        public static string GetPhysicalUploadedFilePath(string filename, string album)
        {
            string albumPath = GetPhysicalUploadPathForAlbum(album);
            return string.Format("{0}\\{1}", albumPath, filename);
        }

        /// <summary>
        /// Get the thumb path for the uploaded picture
        /// </summary>
        /// <param name="albumName"></param>
        /// <param name="uploadedFilename"></param>
        /// <returns></returns>
        public static string GetPhysicalThumbPathForAlbum(string albumName, string uploadedFilename)
        {
            var fileToken = uploadedFilename.Split('.');

            string thumbFileName = string.Format("{0}{1}.{2}", fileToken.ElementAt(0), ConfigKeys.ThumbSufix, fileToken.ElementAt(1));
            string thumbPath = string.Format("~/{0}/{1}/{2}", ConfigKeys.UploadRoot, albumName, thumbFileName);

            return HttpContext.Current.Server.MapPath(thumbPath);

        }


        public static string GetRelativeThumbPath(string albumName, string uploadedFilename)
        {
            var fileToken = uploadedFilename.Split('.');

            string thumbFileName = string.Format("{0}{1}.{2}", fileToken.ElementAt(0), ConfigKeys.ThumbSufix, fileToken.ElementAt(1));
            string thumbPath = string.Format("../../{0}/{1}/{2}", ConfigKeys.UploadRoot, albumName, thumbFileName);

            return thumbPath;
        }

        public static string GetRelativePhotoPath(string albumName, string uploadedFilename)
        {
            string directoryPath = string.Format("../../{0}/{1}/{2}", ConfigKeys.UploadRoot, albumName, uploadedFilename);
            return directoryPath;
        }
    }
}